/*
 * $Id: Change.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IChangeResult;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.PerforceError;
import net.sourceforge.p4eclipse.core.api.Spec;
import net.sourceforge.p4eclipse.core.api.SpecData;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

/**
  * ClientUser object for the 'p4 change' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Change extends ClientUser implements IPerforceCommand
{
	private Result result = null;
	private MultiStatus status;
	private String changeSpec;

	/**
	  * Convenience method for creating instances of Change.
	  */
	public static final Change newInstanceForOutput(String changeSpec)
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Change(status, changeSpec);
	}

	/**
	  * Convenience method for creating instances of Change.
	  */
	public static final Change newInstanceForInput()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Change(status);
	}
	
	/**
	  * Creates a new instance.
	  */
	private Change(MultiStatus status, String changeSpec)
	{
		super(status);
		this.status = status;
		this.changeSpec = changeSpec;
	}

	/**
	  * Creates a new instance.
	  */
	private Change(MultiStatus status)
	{
		super(status);
		this.status = status;
	}

	/** @see IClientUser#inputData() */
	public String inputData()
	{
		return changeSpec;
	}

	public void outputStat(Map strDict)
	{
		if (strDict.containsKey("specdef") && strDict.containsKey("data")) //$NON-NLS-1$ //$NON-NLS-2$
		{
			PerforceError error = new PerforceError();
			error.init();
			
			result = new Result();
			result.spec.decode((String)strDict.get("specdef"), error); //$NON-NLS-1$
			result.spec.parse((String)strDict.get("data"), result.specData, error, 0); //$NON-NLS-1$
			
			if (error.test())
				status.merge(new Status(IStatus.ERROR, PerforceUIPlugin.ID, error.getGeneric(), error.fmt(), null));

			error.dispose();
		}
	}

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "change"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IChangeResult getResult()
	{
		return result;
	}	

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return status;
	}

	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}
	
	private static class Result implements IChangeResult
	{
		private Spec spec = new Spec();
		private SpecData specData = new SpecData();
		
		/** @see IChangeResult#getChangeSpecData() */
		public SpecData getChangeSpecData() { return specData; }

		/** @see IChangeResult#getChangeSpecDefinition() */
		public Spec getChangeSpecDefinition() { return spec; }

	}
}
